package com.example.zk2_lx;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.media.AudioRouting;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.zk2_lx.adapter.VideoAdapter;
import com.example.zk2_lx.databinding.ActivityMainBinding;
import com.example.zk2_lx.entity.ResultEntity;
import com.example.zk2_lx.entity.VideoEntity;
import com.example.zk2_lx.mvvm.BaseActivity;
import com.example.zk2_lx.video.VideoViewModel;
import com.jaeger.library.StatusBarUtil;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, VideoViewModel> {
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;

    int index;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected Class<VideoViewModel> getViewModel() {
        return VideoViewModel.class;
    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

        viewModel.loveSucc.observe(this, new Observer<ResultEntity>() {
            @Override
            public void onChanged(ResultEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    ImageView iv = (ImageView) videoAdapter.getViewByPosition(index, R.id.iv_dianzan);
                    iv.setImageResource(R.mipmap.ic_launcher_round);
                    ObjectAnimator.ofFloat(iv,"scaleX",1,2,1).setDuration(2000).start();
                    videos.get(index).setDianzan(1);
                }
            }
        });
        viewModel.lovFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

        viewModel.noloveSucc.observe(this, new Observer<ResultEntity>() {
            @Override
            public void onChanged(ResultEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    ImageView iv = (ImageView) videoAdapter.getViewByPosition(index, R.id.iv_dianzan);
                    iv.setImageResource(R.mipmap.ic_launcher);
                    videos.get(index).setDianzan(0);
                }
            }
        });
        viewModel.nolovFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTranslucentForImageView(this,0,null);//沉浸式状态栏
        videoAdapter = new VideoAdapter(videos);
        binding.rv.setAdapter(videoAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.rv.setLayoutManager(linearLayoutManager);
        viewModel.video(1,30);

        PagerSnapHelper helper = new PagerSnapHelper();
        helper.attachToRecyclerView(binding.rv);

        binding.rv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int position = linearLayoutManager.findFirstVisibleItemPosition();
                index = position;
                StandardGSYVideoPlayer gsy = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(position, R.id.gsy);
                if(gsy!=null){
                    gsy.startPlayLogic();
                }
            }
        });
        videoAdapter.addChildClickViewIds(R.id.tv_full,R.id.iv_dianzan);
        videoAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_full:
                        Log.d("sss", "onItemChildClick: 1111");
                        full(position);
                        break;
                    case R.id.iv_dianzan:
                        dianzan(position);
                        break;
                }
            }
        });
    }

    private void dianzan(int position) {
        Log.d("sss", "dianzan: 1111");
        if(SPUtils.getInstance().getString("token").isEmpty()){
            startActivity(new Intent(this,LoginActivity.class));
            return;
        }
        if(videos.get(position).getDianzan()==1){
            //取消点赞
            viewModel.nolove(videos.get(position).getId());
        }else{
            //点赞
            viewModel.love(videos.get(position).getId());
        }
    }


    private void full(int position) {
        StandardGSYVideoPlayer gsy = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(position, R.id.gsy);
        gsy.startWindowFullscreen(this,false,false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }
}